<?
class meta_edit extends form{
var $marr=array();
var $params=array();
	function init() {
		if(empty($GLOBALS['Jlib_page_extra'])) $GLOBALS['Jlib_page_extra'][]=$GLOBALS[REG]->get('/default','page');

		$GLOBALS[REG]->get_section('/system/meta',$this->params);
		$this->marr=explode(',',$this->params['metas']);

		$body=''; $new_rec=''; 
		$this->form_params['name']='meta_edit';
		$posted=isset($_POST[$this->form_params['name']]);
		
		foreach($this->marr as $k=>$meta){
			$itm=str_replace('{name}',$meta,$this->tpl['item']);
			$itm=str_replace('{caption}',$GLOBALS[CM]->run('msg:'.$meta),$itm);
			$body[]=$itm;
		}
		$body=implode($this->tpl['separator'],$body);
		$this->pg=str_replace('{body}',$body,$this->pg);

		//if ($this->ctrl['msg_id']=='' && !$posted) $new_rec=$this->tpl['new'];
		//else $new_rec=str_replace('{id}',$this->ctrl['msg_id'],$this->tpl['old']);

		if (!$posted) $this->data=$this->get_data();
		parent::init();
	}

	function get_data() {
		$ucl=str_replace('{pg}',implode('/',$GLOBALS['Jlib_page_extra']),$this->params['ucl_get']);
		$ucl=str_replace('{lang}',$GLOBALS['Jlib_lang'],$ucl);
		$data=$GLOBALS[CM]->run( $ucl );
		if (empty($data)) return array();
		$ret=array();
		//echo '<pre>',print_r( $data ,true),'</pre>';
		foreach ($data as $dt) {
			$ret[$dt['mt_typ']]=$dt['mt_cnt'];
			$ret['ovr_'.$dt['mt_typ']]=$dt['mt_ovr'];
			$ret['XX_'.$dt['mt_typ']]=(!empty($dt['ln']) && $dt['ln']=='XX')?1:0;
		}
		return $ret;
	}

	function do_save() {
		$this->old_data=$this->get_data();
		$pg=implode('/',$GLOBALS['Jlib_page_extra']);
		$ucl_arr=str_replace('{pg}',$pg,$this->params['ucl_set']);
		$ucl_arr=str_replace('{lang}',$GLOBALS['Jlib_lang'],$ucl_arr);
		$ucl_arr=ucl_explode($ucl_arr);
//echo '<pre>',print_r( $this->data ,true),'</pre>';
//echo '<pre>',print_r( $this->old_data ,true),'</pre>';

		foreach ($this->marr as $k=>$meta) {
			$ucl=$ucl_arr;
			$ucl['query'].=" AND mt_typ='$meta'";
			
			if (!isset($this->old_data[$meta]) && !empty($this->data[$meta])){
				$GLOBALS[CM]->run($ucl,'insert',array(
					'mt_pg'=>$pg,
					'ln'=>(empty($this->data['XX_'.$meta]))?$GLOBALS['Jlib_lang']:$this->data['XX_'.$meta],
					'mt_typ'=>$meta,
					'mt_cnt'=>$this->data[$meta],
					'mt_ovr'=>(empty($this->data['ovr_'.$meta]))?0:$this->data['ovr_'.$meta]
				));
			//}elseif(!empty($this->data[$meta]) && ($this->data[$meta]!=$this->old_data[$meta] || $this->data['ovr_'.$meta]!=$this->old_data['ovr_'.$meta])){
			}elseif(!empty($this->data[$meta]) ){
				if(!empty( $this->old_data['XX_'.$meta] )){
					$ucl['query'].=' AND ln=\'XX\'';
					$ucl['params']['auto_query']='no';
				}
				$GLOBALS[CM]->run($ucl,'update',array(
					'ln'=>(empty($this->data['XX_'.$meta]))?$GLOBALS['Jlib_lang']:'XX',
					'mt_cnt'=>$this->data[$meta],
					'mt_ovr'=>(empty($this->data['ovr_'.$meta]))?0:$this->data['ovr_'.$meta]
				));
			}elseif(empty($this->data[$meta]) && !empty($this->old_data[$meta]))
				$GLOBALS[CM]->run($ucl,'delete');
		}
		$this->pg=str_replace('<!--msg-->',$GLOBALS[CM]->run('msg:meta_saved'),$this->pg);
	}
}
?>